' this event fires when the Edit button is clicked
Private Sub dgrTitles_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.EditCommand
' Enter the edit mode and rebind the control.
dgrTitles.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
' this event handler fires when the Cancel button is clicked
Private Sub dgrTitles_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.CancelCommand
' Exit edit mode.
dgrTitles.EditItemIndex = -1
BindDataGrid()
End Sub
' this event handler fires when the Delete button is clicked
Private Sub dgrTitles_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.DeleteCommand
' Get a reference to the DataGridItem being deleted.
Dim dgi As DataGridItem = dgrTitles.Items(e.Item.ItemIndex)
' Get the DataRow with the corresponding key value.
Dim dr As DataRow = GetDataRow(dgrTitles.DataKeys(e.Item.ItemIndex).ToString)
' delete it and rebind the control.
dr.Delete()
BindDataGrid()
End Sub
' this event handler fires when the Update button is clicked
Private Sub dgrTitles_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.UpdateCommand
' Get a reference to the DataGridItem being edited.
Dim dgi As DataGridItem = dgrTitles.Items(e.Item.ItemIndex)
' Get the DataRow with the corresponding key value.
Dim dr As DataRow = GetDataRow(dgrTitles.DataKeys(e.Item.ItemIndex).ToString)
' Update DataRow columns. To do that, we must read data
' stored in the fields contained in the template
' The title field comes from the first (only) control in the second cell in this row.
' This auxiliary routine returns the DataRow with a given key value.
Function GetDataRow(ByVal id As String) As DataRow
' Select the DataTable rows with this key value.
Dim drows() As DataRow = ds.Tables("Titles").Select("title_id='" & id & "'")
' Return the DataRow if found.
If drows.Length > 0 Then
Return drows(0)
End If
End Function
' this routine shows how you can work with all the "selected" rows
' (a selected row is a row whose first column contains a selected checkbox)
Private Sub btnEval_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEval.Click
' Purchase all titles that have a selected checkbox.
Dim totalPrice As Decimal
Dim count As Integer
Dim dgi As DataGridItem
For Each dgi In dgrTitles.Items
' Get a reference to the CheckBox control in this item.
Dim cb As CheckBox = DirectCast(dgi.FindControl("chkSelect"), CheckBox)
' If checked, delete this element.
If cb.Checked Then
' Get the title_id key value for this row.
Dim id As String = dgrTitles.DataKeys(dgi.ItemIndex).ToString
' Select the DataTable row with this key value.
Dim dr As DataRow = GetDataRow(id)
If Not dr.IsNull("price") Then
' Add the price to the running total.
totalPrice += CDec(dr("price"))
count += 1
End If
End If
Next
' Display the total
lblTotal.Text = String.Format("Total price for {0} selected book(s) is ${1}", count, totalPrice)
End Sub
' this event handler fires when the Update button is clicked
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
' Update the Pubs database.
' (this demo doesn't do anything)
End Sub
' this event handler fires when a hyperlink in a column header is clicked
Private Sub dgrTitles_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgrTitles.SortCommand
' Extract current sort column and order.
Dim currSortExpr As String = dgrTitles.Attributes("SortExpr")
Dim newSortExpr As String = e.SortExpression
' If the sort expression is the same as the current one, just reverse the direction.
If Not (currSortExpr Is Nothing) AndAlso currSortExpr.ToString = e.SortExpression Then